package com.matrix.rabbitmqtest.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 生产者
 */
public class Producer {

    public static final String QUEUE_NAME = "BUFFER";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 连接到MQ
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        Connection connection = connectionFactory.newConnection();
        // 从连接中获取信道
        Channel channel = connection.createChannel();
        // 创建一个队列,
        // 参数: 队列名,消息持久化,运行多个消费共享,最后一个连接断开后自动删除队列,其他参数
        // 默认: "",false,true,true,<Map>null
        channel.queueDeclare(QUEUE_NAME, false, false, true, (Map) null);
        // 发送一个消息
        // 参数: 交换机名, 路由名(队列名), 其他参数, 具体消息
        channel.basicPublish("",QUEUE_NAME,null,"Hello World".getBytes(StandardCharsets.UTF_8));
        System.out.println("发送完毕");


    }


}
